package com.xkstream.client;

import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.os.HandlerThread;

/**
 * Created by realuei on 2017/6/29.
 */

public class ZegoAppHelper {

    static final public long RTMP_APP_ID = 1L;

    static final public long UDP_APP_ID = 1196318866;

    static final public long INTERNATIONAL_APP_ID = 3322882036L;

    static final private byte[] RTMP_SIGN_KEY = new byte[]{(byte) 0x91, (byte) 0x93, (byte) 0xcc, (byte) 0x66, (byte) 0x2a, (byte) 0x1c, (byte) 0x0e, (byte) 0xc1,
            (byte) 0x35, (byte) 0xec, (byte) 0x71, (byte) 0xfb, (byte) 0x07, (byte) 0x19, (byte) 0x4b, (byte) 0x38,
            (byte) 0x41, (byte) 0xd4, (byte) 0xad, (byte) 0x83, (byte) 0x78, (byte) 0xf2, (byte) 0x59, (byte) 0x90,
            (byte) 0xe0, (byte) 0xa4, (byte) 0x0c, (byte) 0x7f, (byte) 0xf4, (byte) 0x28, (byte) 0x41, (byte) 0xf7};

    static final public byte[] UDP_SIGN_KEY = {(byte) 0x0e, (byte) 0x5d, (byte) 0x53, (byte) 0x13, (byte) 0x4e, (byte) 0xf0, (byte) 0x86, (byte) 0xa4, (byte) 0x66,
            (byte) 0x43, (byte) 0x13, (byte) 0xab, (byte) 0xd2, (byte) 0x32, (byte) 0x98, (byte) 0xd0,
            (byte) 0x34, (byte) 0x49, (byte) 0x78, (byte) 0xdd, (byte) 0x22, (byte) 0xe8, (byte) 0xa8, (byte) 0xe2, (byte) 0x85,
            (byte) 0x6d, (byte) 0x26, (byte) 0x9c, (byte) 0x3d, (byte) 0x04, (byte) 0x71, (byte) 0x96};

    static final private byte[] INTERNATIONAL_SIGN_KEY = new byte[]{(byte) 0x5d, (byte) 0xe6, (byte) 0x83, (byte) 0xac, (byte) 0xa4, (byte) 0xe5, (byte) 0xad, (byte) 0x43,
            (byte) 0xe5, (byte) 0xea, (byte) 0xe3, (byte) 0x70, (byte) 0x6b, (byte) 0xe0, (byte) 0x77, (byte) 0xa4,
            (byte) 0x18, (byte) 0x79, (byte) 0x38, (byte) 0x31, (byte) 0x2e, (byte) 0xcc, (byte) 0x17, (byte) 0x19,
            (byte) 0x32, (byte) 0xd2, (byte) 0xfe, (byte) 0x22, (byte) 0x5b, (byte) 0x6b, (byte) 0x2b, (byte) 0x2f};

    private static ZegoAppHelper sInstance = new ZegoAppHelper();

    private HandlerThread _backgroundThread = new HandlerThread("zego_background");
    private Handler backgroundHandler;

    private ZegoAppHelper() {
        _backgroundThread.start();
        backgroundHandler = new Handler(_backgroundThread.getLooper());
    }

    /**
     * 在后台执行一个任务，相比普通 Thread，可以对所有待执行的任务排序
     *
     * @param runnable
     */
    static public void postTask(Runnable runnable) {
        sInstance.backgroundHandler.post(runnable);
    }

    /**
     * 移除所有后台任务
     */
    static public void removeTask(Runnable runnable) {
        sInstance.backgroundHandler.removeCallbacks(runnable);
    }

    static public boolean isInternationalProduct(long appId) {
        return appId == INTERNATIONAL_APP_ID;
    }

    static public boolean isUdpProduct(long appId) {
        return appId == UDP_APP_ID;
    }

    static public boolean isRtmpProduct(long appId) {
        return appId == RTMP_APP_ID;
    }

    static public byte[] requestSignKey(long appId) {
        if (isRtmpProduct(appId)) {
            return RTMP_SIGN_KEY;
        } else if (isUdpProduct(appId)) {
            return UDP_SIGN_KEY;
        } else if (isInternationalProduct(appId)) {
            return INTERNATIONAL_SIGN_KEY;
        }
        return null;
    }

}
